//针对博客表

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class BlogDao {
    //插入
    public void insert(Blog blog) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
           connection =  DBUtil.getConnection();
           String sql = "insert into blog values(null, ?, ?, now(), ?)";
           statement = connection.prepareStatement(sql);
           statement.setString(1,blog.getTitle());
           statement.setString(2,blog.getContent());
           statement.setInt(3,blog.getUserId());

           int ret = statement.executeUpdate();
           if(ret == 1) {
               System.out.println("博客插入成功");
           } else {
               System.out.println("插入失败!");
           }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,null);
        }
    }

    //查询
    public Blog selectOne(int blogId) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            connection = DBUtil.getConnection();
            String sql = "select * from blog where blogId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,blogId);

            resultSet = statement.executeQuery();

            if(resultSet.next()) {
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blog.setUserId(resultSet.getInt("userId"));
                return blog;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return null;
    }

    //查询博客列表
    public List<Blog> selectAll() {
        List<Blog> blogs = new ArrayList<>();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            connection = DBUtil.getConnection();
            String sql = "select * from blog order by postTime desc";
            statement = connection.prepareStatement(sql);


            resultSet = statement.executeQuery();

            while(resultSet.next()) {
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                String content = resultSet.getString("content");
                if(content.length() > 100 ) {
                    content = content.substring(0,100);
                }
                blog.setContent(content);
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blog.setUserId(resultSet.getInt("userId"));
                blogs.add(blog);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return blogs;

    }

    //删除
    public  void delete(int blogId) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection =  DBUtil.getConnection();
            String sql = "delete from blog where blogId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,blogId);

            int ret = statement.executeUpdate();
            if(ret == 1) {
                System.out.println("博客删除成功!");
            } else {
                System.out.println("博客删除失败!");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,null);
        }
    }
}
